<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\model\User as UserModel;
/**
 * 后台登录控制器
 */
class Login extends BaseController
{
	/**
	 * 后台用户登录
	 */
	public function index(){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'username|用户名' => 'require',
				'password|密码'  => 'require',
			]);
			//验证失败 输出错误信息
			if(true !== $result) $this->error($result);
			$captcha = $data['captcha'];
			$captcha == '' && $this->error('请输入验证码');
			if(!captcha_check($captcha)){
 				//验证失败
 				$this->error('验证码错误');
			};

			//登录
			$model = new UserModel;
			if($uid = $model->login($data['username'],$data['password'])){
				$this->success('登录成功!',(string)url('index/index'));
			}else{
				$this->error($model->getError());
			}
		}else{
			return $this->fetch();
		}
	}

	/**
	 * 退出登录
	 */
	public function logout(){
        if(is_login()){
            session('user_auth', null);
            session('user_auth_sign', null);
            session('[destroy]');
            $this->success('退出登录!', url('Login/index'));
        }else{
            $this->redirect('Login/index');
        }
    }
}
